// pages/my_account_addr_list/index.js
import WxRequest from '../../utils/wx-request/lib/index.js'

var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        rows: null
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

    },
    toEditPage: function(e) {
        //进入编辑页面
        var obj = e.currentTarget.dataset.obj;
        console.info("e.data.id = %o", obj)
        //将当前需要编辑的数据缓存，到编辑页面直接取缓存即可
        wx.setStorageSync("my_account_addr_edit_obj", obj);
        //跳转到编辑页面
        wx.navigateTo({
            url: '/pages/my_account_addr_edit/index',
        })
    },
    toAddPage: function() {
        //进入添加页面
        wx.navigateTo({
            url: '/pages/my_account_addr_edit/index',
        })
    },
    onShow: function() {
        var that = this;

        app.WxRequest
            .getRequest('/wx/system/deliveryAddr/list?limit=10&offset=0',{
                data:{
                    keys: '',
                    loading: true
                }
            })
            .then(res => {
                console.log(res)
                that.setData({
                    rows: res.data.rows
                })
            })

    },
    setDefaultAddr: function(e) {
        //设置默认收货地址
        wx.showLoading();
        //获取到当前点击的 radio-group value
        var id = e.detail.value;
        //封装好后台参数
        var param = {
            id: id,
            isDefault: "1"
        }
        //更新数据
        wx.request({
            url: app.server.addr + '/wx/system/deliveryAddr/update',
            method: 'POST',
            data: param,
            header: {
                'content-type': 'application/json',
                'sessionId': wx.getStorageSync('sessionId')
            },
            success: function(res) {
                console.log(res)
                wx.showToast({
                    title: res.data.msg
                })
            },
            complete: function() {
                setTimeout(function() {
                    wx.hideLoading();
                }, 200)
            }
        })
    }
})